param (
    [string]$User="",
    [string]$User_pwd="",
    [string]$NodeName=""
)

<#
连接失败、执行命令失败、不在域中（可能是不存在该服务器）返回 False
单机返回 Single
DAG组返回 DAG
#>

$DATA_BACKUP_AGENT_HOME = (Get-ChildItem env:DATA_BACKUP_AGENT_HOME).Value
$Location = $DATA_BACKUP_AGENT_HOME + "\DataBackup\ProtectClient\Plugins\GeneralDBPlugin\bin\applications\exchange\ps1_scripts"
Set-Location -Path $Location
[string]$common_file_path = (Get-Location).Path + "\Common.ps1"
[string]$pssession_name = "CheckNodeType"
[string]$Single = "Single"
[string]$DAG = "DAG"

# 引用Common.ps1
. $common_file_path

if (!(NewConnection $User $User_Pwd $pssession_name)) {
    return $False
}

$ExchangeServers = Invoke-Command -Session $Session -ScriptBlock {Get-ExchangeServer}
# 域中服务器非空判断
if (!$? -or $null -eq $ExchangeServers) {
    ClosePSSession $PSSession_Name
    return $False
}
# Node是否在域中
$total_servers = [string[]]($ExchangeServers.Name)
$isServerInServers = $False
foreach ($server_name in $total_servers) {
    if ($server_name -eq $NodeName) {
        $isServerInServers = $True
        break
    }
}
if (!$isServerInServers) {
    ClosePSSession $PSSession_Name
    return $False
}
# 是否在DAG组中
$dag_groups = Invoke-Command -Session $Session -ScriptBlock {Get-DatabaseAvailabilityGroup}
# DAG非空判断
if ($null -eq $dag_groups) {
    ClosePSSession $PSSession_Name
    return $Single
}
foreach ($dag_group in $dag_groups) {
    $servers = [string[]]$dag_group.Servers
    if ($null -eq $servers) {
        continue
    }
    foreach ($server in $servers) {
        if ($server -eq $NodeName) {
            ClosePSSession $PSSession_Name
            return $DAG
        }
    }
}
ClosePSSession $PSSession_Name
return $Single